#include "GameAppPch.h"

#include "Console.h"
#include "RenderFacade.h"
#include "FontManager.h"
#include "Font.h"

CConsole::CConsole(void)
: m_pConsoleFont( NULL )
{
	m_vecConsoleString.clear();
}

CConsole::~CConsole(void)
{
}

void CConsole::Init()
{
	if( RenderFacade::GetInstance() )
	{
		if( RenderFacade::GetInstance()->GetFontManager() )
		{
			m_pConsoleFont = RenderFacade::GetInstance()->GetFontManager()->GetFont( "test" );
		}
	}

	ClearFile("log.txt");
	WriteToLogFile("LogStart");
	WriteToLogFile("%d",timeGetTime());

}

void CConsole::UnInit()
{
	m_pConsoleFont = NULL;
	m_vecConsoleString.clear();

	Release();
}

void CConsole::WriteToScreen( const LONG& uPosX, const LONG& uPosY, const char* pFormat, ... )
{
	char acBuf[ 512 ] = {0,};

	va_list lst;
	va_start( lst, pFormat );
	vsprintf_s( acBuf, sizeof( char ) * 512, pFormat, lst );
	
	va_end( lst );

	sData data;
	data.m_lPos[ 0 ] = uPosX;
	data.m_lPos[ 1 ] = uPosY;
	data.m_strContext = acBuf;

	m_vecConsoleString.push_back( data );
}

void CConsole::WriteToLogFile( const char* pFormat, ... )
{
	FILE* fp = NULL;
	errno_t no = fopen_s( &fp, "log.txt","at" );
	if( !no )
	{
		va_list lst;
		va_start( lst, pFormat );
		fprintf( fp, pFormat, lst );
		va_end( lst );
		fclose( fp );
	}
}

void CConsole::ClearFile(const char* pFileName )
{
	FILE* fp = NULL;
	errno_t no = fopen_s( &fp, pFileName, "wt" );
	if( !no )
	{
		fclose( fp );
	}
}

void CConsole::Update( const float& fTime )
{
}

void CConsole::Render()
{
	for( size_t ic = 0; ic < m_vecConsoleString.size(); ++ic )
	{
		if( m_pConsoleFont )
		{
			m_pConsoleFont->RenderTextDirect( 
				m_vecConsoleString[ ic ].m_lPos[ 0 ], 
				m_vecConsoleString[ ic ].m_lPos[ 1 ],
				m_vecConsoleString[ ic ].m_strContext.c_str() );
		}
	}
	m_vecConsoleString.clear();
}
